﻿/* Copyright © 2008 by Marcus Bengtsson - makkbe@gmail.com   
 * 
 * This file is part of TaskDispatcher.
 *
 * TaskDispatcher is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * TaskDispatcher is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TaskDispatcher.  If not, see <http://www.gnu.org/licenses/>.
 **/

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Dispatcher.Interfaces;
using Dispatcher.Logging;

namespace Dispatcher.Dispatching
{
    public class DispatchTrigger : IDisposable
    {
        private IDispatcher dispatcher;
        private Timer timer;
        private static readonly int INTERVAL = 60000;
        private int ticks = DispatchTrigger.INTERVAL;

        /// <summary>
        /// Creates a new DispatchTrigger
        /// </summary>
        /// <param name="dispatcher">The IDispatcher to use</param>
        public DispatchTrigger(IDispatcher dispatcher)
        {
            this.dispatcher = dispatcher;
            TimerCallback timerDelegate = new TimerCallback(Dispatch);

            timer = new Timer(timerDelegate, null, System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
        }

        /// <summary>
        /// Dispatch tasks in the set IDispatcher
        /// </summary>
        /// <param name="o"></param>
        public void Dispatch(Object o)
        {
            if (dispatcher == null)
            {
                Logger.Instance.Log("Dispatcher failed to start", System.Diagnostics.EventLogEntryType.FailureAudit);
            }

            else
            {
                Logger.Instance.Log("Dispatcher started", System.Diagnostics.EventLogEntryType.SuccessAudit);
                Array intervals = Enum.GetValues(typeof(DispatchInterval));

                foreach (int i in intervals)
                {
                    if ((i == ticks) && (i != DispatchTrigger.INTERVAL))
                    {
                        DispatchInterval currentInterval = (DispatchInterval)Enum.Parse(typeof(DispatchInterval), Enum.GetName(typeof(DispatchInterval), i), true);
                        dispatcher.DispatchTasks(currentInterval);

                        if (currentInterval == DispatchInterval.WEEKLY)
                        {
                            ticks = DispatchTrigger.INTERVAL;
                        }
                    }                    
                }

                dispatcher.DispatchTasks(DispatchInterval.MINUTELY);
                ticks++;
            }
        }

        /// <summary>
        /// Start the DispatchTrigger
        /// </summary>
        public void Start()
        {
            timer.Change(0, DispatchTrigger.INTERVAL);
        }

        /// <summary>
        /// Stop the DispatchTrigger
        /// </summary>
        public void Stop()
        {
            timer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
        }


        #region IDisposable Members

        public void Dispose()
        {
            Logger.Instance.Log("Disposing dispatcher", System.Diagnostics.EventLogEntryType.Information);
            timer.Dispose();
        }

        #endregion
    }
}
